package weather_service;

import weather_service.weatherprovider.*;
import weather_service.weatherprovider.locationdatamodel.*;
import weather_service.weatherprovider.weatherdatamodel.*;

import java.io.*;

/**
 * This class tests all the functionality provided by the WeatherGateway class
 */
public class TestWeatherGateway {

  public static String partnerID = "1183482959";
  public static String licenseKey = "f00bd621356e77d2";

  public static void main(String[] args) {
    TestWeatherGateway gateway = new TestWeatherGateway();

    //:::::::::::::::::::::::::::::::::::::::::::::::
    // testing "search" service
    //:::::::::::::::::::::::::::::::::::::::::::::::
    System.out.println("\n:: looking up location 20166 :: should work ok");
    gateway.testSearchService("20166");

    System.out.println("\n:: looking up location 20166x :: should work ok, and return empty SearchLocations");
    gateway.testSearchService("20166x");

    System.out.println("\n:: looking up location amsterdam :: should work ok, and return multiple SearchLocations");
    gateway.testSearchService("amsterdam");

    System.out.println("\n:: looking up location la jolla :: should work ok, and return a single SearchLocation");
    gateway.testSearchService("la jolla");

    //:::::::::::::::::::::::::::::::::::::::::::::::
    // testing "local" service
    //:::::::::::::::::::::::::::::::::::::::::::::::
    System.out.println("\n:: checking 20166x weather :: WeatherErorr exception should be thrown");
    gateway.testLocalService("20166x", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking 20166 weather :: should work ok ");
    gateway.testLocalService("95051", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking BGXX0014 weather :: should work ok");
    gateway.testLocalService("BGXX0014", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking \"\" weather :: WeatherError exception should be thrown");
    gateway.testLocalService("", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking invalid locationID weather :: WeatherError exception should be thrown");
    gateway.testLocalService("asdfadf", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking invalid server weather :: IOException should be thrown ");
    gateway.testLocalService("asdfadf", "http://afdjalkfjefi");
  }

  public void testSearchService( String where ){
    WeatherGateway gateway = WeatherGateway.getDefaultInstance();

    try {
      SearchLocations locations = gateway.searchForLocations( where );

      System.out.printf(":::: SearchLocations ::::\n%s\n::::\n", locations);

    } catch (WeatherError e) {
      System.out.printf("---- Exception reported from parsing weather/response, %s ----\n", e);
    } catch (IOException e) {
      System.out.printf("---- Network connection problem or invalid server address, %s ----\n", e);
    }
  }

  public void testLocalService(String locationID, String weatherServer) {
    WeatherConfigurationIF config = new DefaultWeatherConfigurationBean(
        weatherServer, partnerID, licenseKey);
    WeatherGateway gateway = new WeatherGateway(config);

    WeatherReport weather = new WeatherReport();

    // multiple calls to exercise the cache
    for (int i = 0; i < 1; ++i) {
      try {
        weather = gateway.getFullForecast(locationID, 3);
        //weather = gateway.getLocation(locationID);
        //weather = gateway.getCurrentConditions(locationID);

        if (weather == null) {
          System.out.printf("locationID [%s] is not valid or no xml data returned, weather is null\n\n", locationID);
          return;
        }

        System.out.printf(":::: WeatherReport ::::\n%s\n::::\n", weather);

      } catch (IOException e) {
        System.out.printf("---- Network connection problem or invalid server address, %s ----\n", e);
        return;
      } catch (WeatherError e){
        System.out.printf("---- Exception reported from parsing weather/response, %s ----\n", e);
        return;
      }
    }
  }

}//end class TestWeatherGateway

